<template>
	<view :data-theme="theme">
		<view class='integral-details'>
			<view class='header'>
				<view class='currentScore'>当前乐豆</view>
				<view class="scoreNum">{{beans.beans||0}}</view>
				<view class='line'></view>
				<view class='nav acea-row'>
					<view class='item'>
						<view class='num'>{{beans.settledBeans||0}}</view>
						<view>累计乐豆</view>
					</view>
					<view class='item'>
						<view class='num'>{{beans.useBeans||0}}</view>
						<view>累计消费</view>
					</view>
					<view class='item'>
						<view class='num'>{{beans.freezeBeans||0}}</view>
						<view>冻结乐豆</view>
					</view>
				</view>
			</view>
			<view class='wrapper'>
				<view class='nav acea-row'>
					<view class='item acea-row row-center-wrapper' style="color: #333;" :class='current==index?"on":""'
						v-for="(item,index) in navList" :key='index' @click='nav(index)'><text class='iconfont'
							:class="item.icon"></text>{{item.name}}</view>
				</view>
				<view class='list' :hidden='current!=0'>
					<view class='item acea-row row-between-wrapper' v-for="(item,index) in beansList" :key="index">
						<view>
							<view class='state'>{{ GetType(item.type) }}</view>
							<view>{{item.createTime}}</view>
						</view>
						<view class='num font_color' v-if="[2,3,5,7].includes(item.type)">+{{item.amount}}</view>
						<view class='num' v-else>-{{item.amount}}</view>
					</view>
					<view class='loadingicon acea-row row-center-wrapper' v-if="beansList.length>0">
						<text class='loading iconfont icon-jiazai' :hidden='loading==false'></text>{{loadTitle}}
					</view>
					<view class='noCart' v-if="beansList.length == 0">
						<view class='pictrue'>
							<image :src="urlDomain+'crmebimage/presets/noJilu.png'"></image>
							<view class="default_txt">暂无乐豆记录哦~</view>
						</view>
					</view>
				</view>
				<view class='score-detail' :hidden='current!=1'>
					<view class="score-btn" @click="onTransfer(1)">乐豆赠送</view>
					<view class="score-btn" @click="onTransfer(2)">乐豆提现</view>
					<view class="score-btn" @click="onTransfer(3)">乐豆转积分</view>
					<view class="score-btn" @click="onTransfer(4)">乐豆转通证</view>
				</view>

				<tui-drawer mode="bottom" :visible="assureDrawer" @close="closeAssure">
					<view class="ensure">
						<view @click="closeAssure" class="title1"><text class="iconfont icon-guanbi5"></text></view>
						<block v-if="types === 1">
							<input v-model="phone" class="en-input" placeholder="请输入手机号" />
							<input v-model="amount" class="en-input" placeholder="请输入额度" />
							<view class="en-btn" @click="onsubmit">确认转账</view>
						</block>
						<block v-else-if="types === 2">
							<view v-if="beans.isBeansWithdrawFee" class='default_txt'>
								手续费(%)：{{beans.beansWithdrawFeeRate}}</view>
							<input v-model="amount" class="en-input" placeholder="请输入额度" />
							<!-- <label v-for="option in options" :key="option.value">
								<input type="radio" :value="option.value" v-model="bankType">{{ option.label }}
							  </label> -->
							<radio-group @change="radioChange" style="margin: 10px 0px 10px 20px;">
								<label class="radio" style="margin-left: 10px;" v-for="(item, index) in options" :key="index">
									<radio :value="item.value" :checked="item.checked" />{{ item.label }}
								</label>
							</radio-group>
							<view class="en-btn" style="margin-top: 45px;" @click="onwithdraw">确认提现</view>
						</block>
						<block v-else-if="types === 3">
							<input v-model="amount" class="en-input" placeholder="请输入额度" />
							<view class="en-btn" style="margin-top: 80px;" @click="exchangeScore">乐豆转积分</view>
						</block>
						<block v-else>
							<view class='default_txt'>当前比例：{{beans.exchangeTokenRate}}</view>
							<input v-model="amount" class="en-input" placeholder="请输入额度" />
							<view class="en-btn" style="margin-top: 80px;" @click="exchangeToken">乐豆转通证</view>
						</block>
					</view>
				</tui-drawer>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getbeansList,
		getbeansrecord,
		transferbeans,
		exchangeToken,
		exchangeIntegral,
		withdrawbeans
	} from "@/api/new.js"
	import {
		toLogin
	} from '@/libs/login.js';
	import {
		mapGetters
	} from "vuex";
	let app = getApp();
	export default {
		data() {
			return {
				urlDomain: this.$Cache.get("imgHost"),
				navList: [{
						'name': '乐豆明细',
						'icon': 'icon-mingxi'
					},
					{
						'name': '乐豆操作',
						'icon': 'icon-tishengfenzhi'
					}
				],
				current: 0,
				page: 1,
				limit: 10,
				beansList: [],
				beans: {},
				assureDrawer: false,
				types: 1,
				phone: '',
				amount: 0,
				bankType: 1,
				loadend: false,
				loading: false,
				loadTitle: '加载更多',
				options: [ // 单选按钮选项列表
					{
						value: 1,
						label: '银行卡',
						checked: true
					},
					{
						value: 2,
						label: '微信',
						checked: false
					},
					{
						value: 3,
						label: '支付宝',
						checked: false
					}
				],
				theme: app.globalData.theme,
			};
		},
		computed: mapGetters(['isLogin']),
		watch: {
			isLogin: {
				handler: function(newV, oldV) {
					if (newV) {
						this.getUserInfo();
						this.getBeansList();
					}
				},
				deep: true
			}
		},
		onLoad() {
			if (this.isLogin) {
				this.getUserInfo();
				this.getBeansList();
			} else {
				toLogin();
			}
		},
		/**
		 * 页面上拉触底事件的处理函数
		 */
		onReachBottom: function() {
			this.getBeansList();
		},
		methods: {
			getUserInfo: function() {
				let that = this;
				getbeansList().then(function(res) {
					that.$set(that, 'beans', res.data);
				});
			},
			onTransfer(item) {
				this.types = item
				this.assureDrawer = true
			},
			closeAssure() {
				this.assureDrawer = false;
			},
			// 转账
			onsubmit: function() {
				if (this.loading) {
					return;
				}
				this.loading = true;
				let that = this
				const obj = {
					amount: that.amount,
					phone: that.phone
				}
				transferbeans(obj).then(res => {
					if (res.code === 200) {
						that.$util.Tips({
							title: "成功"
						});
						that.assureDrawer = false;
						this.loading = false;
					}
				}).catch(err => {
					that.$util.Tips({
						title: err
					});
					this.loading = false;
				});
			},
			//提现
			onwithdraw: function() {
				if (this.loading) {
					return;
				}
				this.loading = true;
				let that = this
				const obj = {
					amount: that.amount,
					bankType: that.bankType
				}
				withdrawbeans(obj).then(res => {
					if (res.code === 200) {
						that.$util.Tips({
							title: "成功"
						});
						that.assureDrawer = false;
						this.loading = false;
					}
				}).catch(err => {
					that.$util.Tips({
						title: err
					});
					this.loading = false;
				});
			},
			//转积分
			exchangeScore: function() {
				if (this.loading) {
					return;
				}
				this.loading = true;
				let that = this
				const obj = {
					amount: that.amount
				}
				exchangeIntegral(obj).then(res => {
					if (res.code === 200) {
						that.$util.Tips({
							title: "成功"
						});
						that.assureDrawer = false;
						this.loading = false;
					}
				}).catch(err => {
					that.$util.Tips({
						title: err
					});
					this.loading = false;
				});
			},
			//转通证
			exchangeToken: function() {
				if (this.loading) {
					return;
				}
				this.loading = true;
				let that = this
				const obj = {
					amount: that.amount
				}
				exchangeToken(obj).then(res => {
					if (res.code === 200) {
						that.$util.Tips({
							title: "成功"
						});
						that.assureDrawer = false;
						this.loading = false;
					}
				}).catch(err => {
					that.$util.Tips({
						title: err
					});
					this.loading = false;
				});
			},

			/**
			 * 获取乐豆明细
			 */
			getBeansList: function() {
				let that = this;
				if (that.loading) return;
				if (that.loadend) return;
				that.loading = true;
				that.loadTitle = '';
				getbeansrecord({
					page: that.page,
					limit: that.limit
				}).then(function(res) {
					let list = res.data.list,
						loadend = list.length < that.limit;
					that.beansList = that.$util.SplitArray(list, that.beansList);
					that.$set(that, 'beansList', that.beansList);
					that.page = that.page + 1;
					that.loading = false;
					that.loadend = loadend;
					that.loadTitle = loadend ? '哼~😕我也是有底线的~' : "加载更多";
				}, function(res) {
					this.loading = false;
					that.loadTitle = '加载更多';
				});
			},
			GetType(type) {
				// 1-兑换盲盒，2-盲盒收益，3-系统增加，4-系统扣减，5-积分兑换，6-用户转出，7-用户转入，8-提现，9-兑换通证
				let data = ""
				if (type === 1) {
					data = "兑换盲盒"
				} else if (type === 2) {
					data = "盲盒收益"
				} else if (type === 3) {
					data = "系统增加"
				} else if (type === 4) {
					data = "系统扣减"
				} else if (type === 5) {
					data = "积分兑换"
				} else if (type === 6) {
					data = "用户转出"
				} else if (type === 7) {
					data = "用户转入"
				} else if (type === 8) {
					data = "提现"
				} else if (type === 9) {
					data = "兑换通证"
				}
				return data
			},
			nav: function(current) {
				this.current = current;
			},
			radioChange(e) {
				console.log('选中的值:', e.detail.value); // e.detail.value 是选中的值
				this.bankType = e.detail.value
				this.options.forEach(item => {
					item.checked = item.value === e.detail.value;
				});
			}
		}
	}
</script>

<style scoped lang="scss">
	.integral-details .header {
		background-image: url('');
		background-repeat: no-repeat;
		background-size: 100% 218rpx;
		background-position: center bottom;
		width: 100%;
		height: 460rpx;
		font-size: 72rpx;
		color: #fff;
		padding: 31rpx 0 45rpx 0;
		box-sizing: border-box;
		text-align: center;
		font-family: 'Guildford Pro';
		@include main_bg_color(theme);
	}

	.integral-details .header .currentScore {
		font-size: 26rpx;
		color: rgba(255, 255, 255, 0.8);
		text-align: center;
		margin-bottom: 11rpx;
	}

	.integral-details .header .scoreNum {
		font-family: "Guildford Pro";
	}

	.integral-details .header .line {
		width: 60rpx;
		height: 3rpx;
		background-color: #fff;
		margin: 20rpx auto 0 auto;
	}

	.integral-details .header .nav {
		font-size: 22rpx;
		color: rgba(255, 255, 255, 0.8);
		flex: 1;
		margin-top: 35rpx;
	}

	.integral-details .header .nav .item {
		width: 33.33%;
		text-align: center;
	}

	.integral-details .header .nav .item .num {
		color: #fff;
		font-size: 40rpx;
		margin-bottom: 5rpx;
		font-family: 'Guildford Pro';
	}

	.integral-details .wrapper .nav {
		flex: 1;
		width: 690rpx;
		border-radius: 20rpx 20rpx 0 0;
		margin: -96rpx auto 0 auto;
		background-color: #f7f7f7;
		height: 96rpx;
		font-size: 30rpx;
		color: #bbb;
	}

	.integral-details .wrapper .nav .item {
		text-align: center;
		width: 50%;
	}

	.integral-details .wrapper .nav .item.on {
		background-color: #fff;
		@include main-color(theme);
		font-weight: bold;
		border-radius: 20rpx 0 0 0;
	}

	.integral-details .wrapper .nav .item:nth-of-type(2).on {
		border-radius: 0 20rpx 0 0;
	}

	.integral-details .wrapper .nav .item .iconfont {
		font-size: 38rpx;
		margin-right: 10rpx;
	}

	.integral-details .wrapper .list {
		background-color: #fff;
		padding: 24rpx 30rpx;
	}

	.integral-details .wrapper .list .tip {
		font-size: 25rpx;
		width: 690rpx;
		height: 60rpx;
		border-radius: 50rpx;
		background-color: #fff5e2;
		border: 1rpx solid #ffeac1;
		color: #c8a86b;
		padding: 0 20rpx;
		box-sizing: border-box;
		margin-bottom: 24rpx;
	}

	.integral-details .wrapper .list .tip .iconfont {
		font-size: 35rpx;
		margin-right: 15rpx;
	}

	.integral-details .wrapper .list .item {
		height: 124rpx;
		border-bottom: 1rpx solid #eee;
		font-size: 24rpx;
		color: #999;
	}

	.integral-details .wrapper .list .item .state {
		font-size: 28rpx;
		color: #282828;
		margin-bottom: 8rpx;
	}

	.integral-details .wrapper .list .item .num {
		font-size: 36rpx;
		font-family: 'Guildford Pro';
	}

	.integral-details .wrapper .list2 {
		background-color: #fff;
		padding: 24rpx 0;
	}

	.integral-details .wrapper .list2 .item {
		background-image: linear-gradient(to right, #fff7e7 0%, #fffdf9 100%);
		width: 690rpx;
		height: 180rpx;
		position: relative;
		border-radius: 10rpx;
		margin: 0 auto 20rpx auto;
		padding: 0 25rpx 0 180rpx;
		box-sizing: border-box;
	}

	.integral-details .wrapper .list2 .item .pictrue {
		width: 90rpx;
		height: 150rpx;
		position: absolute;
		bottom: 0;
		left: 45rpx;
	}

	.integral-details .wrapper .list2 .item .pictrue image {
		width: 100%;
		height: 100%;
	}

	.integral-details .wrapper .list2 .item .name {
		width: 285rpx;
		font-size: 30rpx;
		font-weight: bold;
		color: #c8a86b;
	}

	.integral-details .wrapper .list2 .item .earn {
		font-size: 26rpx;
		color: #c8a86b;
		border: 2rpx solid #c8a86b;
		text-align: center;
		line-height: 52rpx;
		height: 52rpx;
		width: 160rpx;
		border-radius: 50rpx;
	}

	.font_color {
		@include price_color(theme);
	}

	.noCart {
		margin-top: -62rpx;
		padding-top: 0.1rpx;
		height: 800rpx;
	}

	.noCart .pictrue {
		width: 414rpx;
		height: 336rpx;
		margin: 78rpx auto 56rpx auto;
	}

	.noCart .pictrue image {
		width: 100%;
		height: 100%;
	}

	.default_txt {
		font-size: 26rpx;
		color: #999;
		text-align: center;
	}

	.list {
		min-height: 72vh;
	}

	.score-detail {
		width: 100%;

		.score-btn {
			margin: 0 auto;
			width: 60%;
			height: 40px;
			text-align: center;
			color: #fff;
			line-height: 40px;
			background-color: #f44336;
			border-radius: 10px;
			margin-top: 40px;
		}
	}

	.ensure {
		width: 100%;
		height: 250px;
		background-color: #fff;
		padding-bottom: 22rpx;
		padding-bottom: calc(22rpx+ constant(safe-area-inset-bottom)); ///兼容 IOS<11.2/
		padding-bottom: calc(22rpx + env(safe-area-inset-bottom)); ///兼容 IOS>11.2/

		.title1 {
			font-size: 32rpx;
			text-align: center;
			height: 15px;
			background-color: #fff;
			margin: 20rpx 0 36rpx 0;
			position: relative;

			.iconfont {
				position: absolute;
				right: 30rpx;
				top: 0;
				font-size: 36rpx;
			}
		}

		.en-title {
			padding: 20px 20px 10px 20px;
			font-size: 16px;
			color: #333;

			text {
				color: #FFDEAD;
			}
		}

		.en-input {
			width: 90%;
			margin-left: 20px;
			margin-top: 15px;
			height: 76rpx;
			border: 2rpx solid #eee;
			border-radius: 5rpx;
			padding: 0 24rpx;
			font-size: 32rpx;
		}

		.en-btn {
			width: 80%;
			margin: 0 auto;
			height: 40px;
			margin-top: 25px;
			background-color: #333;
			border-radius: 20px;
			color: #fff;
			text-align: center;
			line-height: 40px;
		}
	}

	.mask {
		z-index: 9
	}

	.ensure.on {
		transform: translate3d(0, 0, 0);
	}
</style>